//输入的第一行为命令列表的长度K
//输入的第二行为文件中的原始文本
//接下来的K行，每行为一个指令
const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
let n;
let text;
let orderArr = [];
rl.on("line", (line)=>{
    if(n === undefined) {
        n = Number(line.trim())
    } else if (text === undefined) {
        text = line.trim()
    } else {
        orderArr.push(line.trim().split(" "))
        if(orderArr.length === n) {
            console.log(getResult(n, text, orderArr))
            // clear
            n = text = undefined
            orderArr.length = 0
        }

    }
})

function getResult(n, text, orderArr) {
    let index = 0
    for (const [order, x] of orderArr) {
        switch (order) {
            case "FORWARD":
                [text, index] = forward(text, index, x)
                break;
            case "BACKWARD":
                [text, index] = backward(text, index, x)
                break;
            case "SEARCH-FORWARD":
                [text, index] = searchForward(text, index, x)
                break;
            case "SEARCH-BACKWARD":
                [text, index] = searchBackward(text, index, x)
                break;
            case "INSERT":
                [text, index] = insertWord(text, index, x)
                break;
            case "REPLACE":
                [text, index] = replaceWord(text, index, x)
                break;
            case "DELETE":
                [text, index] = deleteX(text, index, x)
                break;
        }

    }
    return text

}

function forward(text, index, x){
    index+= x;
    if(index >= text.length){
        index = text.length
    }
    return [text, index]

}
function backward(text, index, x){
    index-= x;
    if(index < 0){
        index = 0
    }
    return [text, index]

}

function searchForward(text, index, word){
   if(text.indexOf(word, index) !== -1) {
       index = text.indexOf(word, index)
   }
    return [text, index]
}

function searchBackward(text, index, word){
    if(text.lastIndexOf(word, index) !== -1) {
        index = text.indexOf(word, index)
    }
    return [text, index]
}
function insertWord(text, index, word){
    text = text.slice(0, index) + word + text.slice(index)
    index = index + word.length
    return [text, index]

}
function replaceWord(text, index, word){
    text = text.slice(0, index) + word + text.slice(index + word.length)
    return [text, index]

}
function deleteX(text, index, x){
    text = text.slice(0, index)  + text.slice(index + x)
    return [text, index]
}